<template>
   <div v-transfer-dom>
      <loading v-model="isLoading"></loading>
    </div>
</template>
<script>
import { Loading,TransferDom } from 'vux'
// 获取用户缓存
import {getStore,setStore} from '@/config/mUtils'
import store from 'store/store'
import {appId,nativeto} from '@/config/config'
import {mapGetters,mapActions} from 'vuex';
export default {
  directives: {
    TransferDom
  },
  components:{
    Loading
  },
  data(){
    return{
      isLoading: true
    }
  },
  computed:{
    ...mapGetters(['getQcode','getWxCode'])
  },
  mounted(){
    if(this.getQcode){
      // let code = getStore('wxcode') ? '' : this.getWxCode
      if(this.getWxCode){
        this.initData('code=',this.getWxCode)
      }else if(getStore('wxcode')){
        this.initData('openId=',getStore('wxcode'))
      }else{
        let that = this;
        this.isLoading = false
        // closeWindow
        this.$vux.confirm.show({
          title: '没有授权',
          content: '请允许授权',
          onCancel () {
            that.$wechat.closeWindow();
          },
          onConfirm () {
            window.location.href= `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${encodeURIComponent(url)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
          }
        });
      }
    }
  },
  methods:{
    ...mapActions([
      'setBindPhone'
    ]),
    initData(urls='openId=',data=getStore('wxcode')){
      this.$http.get(`shop/qrCode/get?${urls}${data}&qrCode=${this.getQcode}`)
        .then(({data})=>{
          if(data.status === '200'){
            // alert(data.data.cellphone)
            // if(getStore('wxcode')){
            //   alert('a');
            //   this.setBindPhone(data.data.cellphone)
            //   this.$router.push({path:'/goods',query:{tableId:data.data.tableId,tableName:data.data.tableName,shopId:data.data.shopId,orderType:data.data.orderType,shopName:data.data.shopName,type:data.data.type,isBack:true}})
            // }else if(data.data.openId){
            //   alert('b')
            //   setStore('wxcode',data.data.openId)
            //   if(data.data.nickname){
            //     setStore('nickname',data.data.nickname)
            //   }
            //   if(data.data.avatar){
            //     setStore('avatar',data.data.avatar)
            //   }
            //   if(!isNaN(data.data.bindMobile)){
            //     setStore('bindMobile',data.data.bindMobile)
            //   }
            this.setBindPhone(data.data.cellphone)
            if(data.data.openId){
							setStore('wxcode',data.data.openId)
						}
						if(data.data.nickname){
							setStore('nickname',data.data.nickname)
						}
						if(data.data.avatar){
							setStore('avatar',data.data.avatar)
						}
						if(!isNaN(data.data.bindMobile)){
							setStore('bindMobile',data.data.bindMobile)
						}
            this.$router.push({path:'/goods',query:{tableId:data.data.tableId,tableName:data.data.tableName,shopId:data.data.shopId,orderType:data.data.orderType,shopName:data.data.shopName,type:data.data.type,isBack:true}})
            // }
          }
        }).catch(err=>{
          if(err.data.status === '10000'){
            this.$vux.toast.show({
              type: 'warn',
              text: err.data.msg,
              time: 2000
            });
            setTimeout(()=>{
              let nativeto_url_find = nativeto+'?val='+this.getQcode;
							window.location.href= `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${encodeURIComponent(nativeto_url_find)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
						},1000)
          }else{
            this.$vux.toast.show({
              type: 'warn',
              text: err.data.msg,
              time: 2000
            });
          }
        });
    }
  }
}
</script>
